/**
 * 我的发帖
 */
var mypost = {
    /**
     * 获取我的所有发帖
     */
    getMyPost:function(){
        if(!getCookie("uuid")){
            layer.alert("请先登陆");
            return;
        }
        var _this = this;
        $.get("/post/getmypost",{uuid : getCookie("uuid")},function(res){
            if("0" == res.code){
                $('#table').bootstrapTable('load',res.data);
            }else {
                layer.alert(res.message);
            }
        })
    },
    /**
     * 删除按钮
     */
    delClk:function(){
        var _this = this;
        $(".del-mypost").click(function(){
            var list = $('#table').bootstrapTable('getAllSelections');
            if(list.length <= 0){
                layer.alert("请选择删除的项");
                return ;
            }
            //询问框
            layer.confirm('确认删除？', {
                btn: ['确认','取消'] //按钮
            }, function(){
                _this.delChoose();//删除选中
            }, function(){
                layer.close();
            });
        })
    },
    /**
     * 删除选中
     */
    delChoose:function(){
        var _this = this;
        var list = $('#table').bootstrapTable('getAllSelections');
        var ids = list.map(function(item){
            return item.postId;
        }).join(",")
        // Assign handlers immediately after making the request,
        // and remember the jqxhr object for this request
        var jqxhr = $.post("/post/delete?ids="+ids+"&uuid="+getCookie("uuid"), function(res) {
        })
        .done(function(res) {
            if("0" == res.code){
                layer.closeAll();
                layer.alert("删除成功")
                _this.getMyPost();//删除成功后重新获取
            }else {
                layer.close();
            }
        })
        .fail(function(error) {
            layer.closeAll();
            layer.alert(error.responseText)
        })
        .always(function() {
        });
        
        // Perform other work here ...
        
        // Set another completion function for the request above
        jqxhr.always(function() {
        });
        layer.close();
        layer.load(1, {
            shade: [0.1,'#fff'] //0.1透明度的白色背景
        });
    },  
    /**
     * 初始化表格
     */
    initTable:function(data){
        var columns = [
            {
                checkbox:true
            },
            {
                field: 'index',
                title: 'id',
                formatter:function(value,row,index){
                    return index + 1;
                },
                clickToSelect: true
            },
            {
                field: "title",
                title: "标题",
                clickToSelect: true
            },
            {
                field: "createTime",
                title: "发布时间",
                sortable: true,
                clickToSelect: true
            },
            {
                field: "textNum",
                title: "字数",
                clickToSelect: true
            },
            {
                field: "typeName",
                title: "分类",
                clickToSelect: true
            },
            {
                field: "descript",
                title: "描述",
                clickToSelect: true
            },
            {
                field: "show",
                title: "查看",
                formatter:function(value,row,index){
                    return "<a href='/detail/"+row.postId+"'>查看详情</a>"
                }
            }
        ]
        $('#table').bootstrapTable({
            columns: columns,
            data: data,
            search: true,
            striped: true,
            showColumns: true,
            formatSearch: function(){
                return "请输入搜索内容";
            },
            formatNoMatches:function(){
                return "没有数据";
            }
        });
    }
}
$(function(){
    mypost.initTable();//初始化表格
    mypost.getMyPost();//初始化获取数据
    mypost.delClk();//删除按钮
})